﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Autofac;
using Autofac.Core;
using Autofac.Configuration;
using Autofac.Builder;
using DemoAutofac.Models;

namespace SimpleDemo {
    public class configureByCode {
        public void buildACar() {
            var builder = new ContainerBuilder();
            builder.RegisterType<ElectricEngine>().As<IEngine>();
            builder.RegisterType<Car>();            
            //Tuong duong voi lenh nay builder.Register(c => new Car(c.Resolve<IEngine>()));

            using (var container = builder.Build()) {
                var car = container.Resolve<Car>();
                car.Name = "Honda";
                car.start();
            }
        }

        public void resolveCarByName() 
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<ElectricEngine>().Named<IEngine>("Electric");

            builder.RegisterType<GasEngine>().Named<IEngine>("Gas");

            //I named the Car with Gas engine is gasCar
            builder.Register(c => new Car(c.Resolve<IEngine>("Gas"))).Named<Car>("gasCar");

            //I named the Car with electric engine is eCar
            builder.Register(c => new Car(c.Resolve<IEngine>("Electric"))).Named<Car>("eCar");
                        
            using (var container = builder.Build()) {
                var eCar = container.Resolve<Car>("eCar");
                eCar.Name = "Honda Prius";
                eCar.start();

                var gCar = container.Resolve<Car>("gasCar");
                gCar.Name = "Porsche Cayne";
                gCar.start();
            }
        }

        public class BullDozer {
            public IEngine engine { get; set; }
        }

        public void demoAutowiring() {
            var builder = new ContainerBuilder();
            builder.RegisterType<ElectricEngine>().As<IEngine>();
            //Try to remove .PropertiesAutowired(); and run again
            builder.RegisterType<BullDozer>().PropertiesAutowired();
            //builder.RegisterType<BullDozer>(); 
            using (var container = builder.Build()) {
                var bullDozer = container.Resolve<BullDozer>();
                bullDozer.engine.start();

            }
        }

        public class Truck {
            public IEngine _engine;
            public string _name;
            public Truck(IEngine engine, string name) {
                _engine = engine;
                _name = name;
            }
        }
        /// <summary>
        /// Demo NamedParameter
        /// </summary>
        public void demoParameterized() {
            var builder = new ContainerBuilder();
            builder.RegisterType<ElectricEngine>().Named<IEngine>("Electric");
            builder.RegisterType<GasEngine>().Named<IEngine>("Gas");

            builder.RegisterType<Truck>();


            using (var container = builder.Build()) {
                var engine = container.Resolve<IEngine>("Electric");

                //Resolve Truck with appropriate constructor
                var truck = container.Resolve<Truck>(
                    new NamedParameter("engine", engine),
                    new NamedParameter("name", "Volvo"));

                Console.Write("{0} : ",truck._name);
                truck._engine.start();
            }
        }

    }
}